var myApp = angular.module("myApp");

var colors = [ '#6f599c', '#8A0060', '#8F76AD', '#C1A3B8', '#9966CC', '#D9BFD2' ];
myApp.controller('firstController',function($scope, mtAjax, mtParseChartData,$filter, $uibModal){
  $scope.select1 = '协议备案';
  $scope.select2 = '按行业';
    $scope.params={
        startDate:$filter('date')(new Date(), "yyyy")+'-01-01',
        endDate:$filter('date')(new Date(), "yyyy-MM-dd")
    };
    $scope.sc=function(){
        var str="ana2?startdate="+$scope.params.startDate+"&enddate="+$scope.params.endDate;
        location.replace(str)
    };
  var arr=[1,2,3];

  $scope.isArray=angular.isArray($scope.name);
  
  $scope.option1 = {
		title : {
            text: '协议备案情况',
            x:'center'
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        tooltip : {
            trigger: 'axis',
            formatter: "{b} 年<br/>{a} : {c} 件<br/>{a1} : {c1} 家<br/>"
        },
        legend: {
            data:['备案协议数量（件）','设计企业数量（家）'],
            y:'30'
        },
        toolbox: {
            show : false,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : false,
        xAxis : [
            {
                type : 'category',
                data : ['2016年','2017年','2018年','2019年']
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'备案协议数量（件）',
                type:'bar',
                data:[2.0, 4.9, 7.0, 23.2],
            },
            {
                name:'设计企业数量（家）',
                type:'bar',
                data:[2.6, 5.9, 9.0, 26.4],
            }]
	};

	$scope.option2 = {
    title : {
            text: '政策兑现情况',
            x:'center'
        },
        tooltip : {
            trigger: 'axis',
            formatter: "{b} 年<br/>{a} : {c} 件<br/>{a1} : {c1} 件<br/>"
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        legend: {
            data:['申报数量','兑现完成数量'],
            y:'30'
        },
        toolbox: {
            show : false,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : false,
        xAxis : [
            {
                type : 'category',
                data : ['2016年','2017年','2018年','2019年']
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'申报数量',
                type:'bar',
                data:[2, 9, 7, 13],
            },
            {
                name:'兑现完成数量',
                type:'bar',
                data:[6, 5, 9, 10],
            }]
    };
    
    $scope.option3 = {
    title : {
            text: '本年协议备案情况',
            x:'center'
        },
        tooltip : {
            trigger: 'axis',
            formatter: "{b}<br/>{a} : {c} 件<br/>{a1} : {c1} 家<br/>"
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        legend: {
            data:['备案协议数量（件）','设计企业数量（家）'],
            y:'30'
        },
        toolbox: {
            show : false,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : false,
        xAxis : [
            {
                type : 'category',
                data : ['2月','3月','4月','5月']
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'备案协议数量（件）',
                type:'bar',
                data:[2.0, 4.9, 7.0, 23.2],
            },
            {
                name:'设计企业数量（家）',
                type:'bar',
                data:[2.6, 5.9, 9.0, 26.4],
            }]
    };
    
    $scope.option4 = {
    title : {
            text: '本年政策兑现情况',
            x:'center'
        },
        tooltip : {
            trigger: 'axis',
            formatter: "{b}<br/>{a} : {c} 件<br/>{a1} : {c1} 件<br/>"
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        legend: {
            data:['申报数量','兑现完成数量'],
            y:'30'
        },
        toolbox: {
            show : false,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : false,
        xAxis : [
            {
                type : 'category',
                data : ['2月','3月','4月','5月']
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'申报数量',
                type:'bar',
                data:[2, 9, 7, 13],
            },
            {
                name:'兑现完成数量',
                type:'bar',
                data:[6, 5, 9, 10],
            }]
    };
    
    $scope.option5 = {
    title: {
        text: '当前兑现工作情况',
         x:'center'
    },
    color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        },
        formatter: function (params) {
            var tar = params[1];
            return tar.name + ': ' + tar.value + ' 件';
        }
    },
    toolbox: {
        show : false,
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    xAxis : [
        {
            type : 'value'
        }
    ],
    yAxis : [
        {
            
            type : 'category',
            splitLine: {show:false},
            data : ['完成申报','完成确认','递交纸质材料','完成兑现']
        }
    ],
    series : [
        {
            name:'辅助',
            type:'bar',
            stack: '总量',
            itemStyle:{
                normal:{
                    barBorderColor:'rgba(0,0,0,0)',
                    color:'rgba(0,0,0,0)'
                },
                emphasis:{
                    barBorderColor:'rgba(0,0,0,0)',
                    color:'rgba(0,0,0,0)'
                }
            },
            data:[0, 4, 5, 7]
        },
        {
            name:'生活费',
            type:'bar',
            stack: '总量',
            itemStyle : { normal: {label : {show: true, position: 'inside'}}},
            data:[4, 1, 2, 1]
        }]
    };
    
    $scope.option6 = {
    title: {
            text: '本年各行业备案情况',
            //subtext: '纯属虚构',
            y: '30',
            x: 'center'
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        tooltip: {
            trigger: 'item',
            formatter: "{b} : {c} 件 ({d}%)"
        },
        /*legend: {
            orient : 'vertical',
            x : 'left',
            data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
        },*/
        toolbox: {
            show: false,
            feature: {
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: {
                    show: true
                },
                saveAsImage: {
                    show: true
                }
            }
        },
        calculable: false,
        series: [
            {
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            textStyle: {
                                fontFamily: 'MicrosoftYaHei',
                                fontSize: '12',
                                fontWeight: 'bold'
                            },
                            formatter: function (params) {
                                return (params.name) + ':' + params.value + ' (件)'
                            }
                        },
                        labelLine: {
                            show: true
                        }
                    },
                },
                data: [
                    {
                        value: 335,
                        name: '备案企业'
                    },
                    {
                        value: 1548,
                        name: '全部企业'
                    }
	            ]
	        }]
    };
    
    $scope.option7 = {
    title: {
            text: '累计各行业备案情况',
            y: '30',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{b} : {c} 件 ({d}%)"
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        /*legend: {
            orient : 'vertical',
            x : 'left',
            data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
        },*/
        toolbox: {
            show: false,
            feature: {
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: {
                    show: true
                },
                saveAsImage: {
                    show: true
                }
            }
        },
        calculable: false,
        series: [
            {
                name: '地区占比',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            textStyle: {
                                fontFamily: 'MicrosoftYaHei',
                                fontSize: '12',
                                fontWeight: 'bold'
                            },
                            formatter: function (params) {
                                return (params.name) + ':' + params.value + ' (件) '
                            }
                        },
                        labelLine: {
                            show: true
                        }
                    },
                },
                data: [
                    {
                        value: 135,
                        name: '制造业'
                    },
                    {
                        value: 234,
                        name: '纺织业'
                    },
                    {
                        value: 310,
                        name: '服务业'
                    },
                    {
                        value: 135,
                        name: '农业'
                    },
                    {
                        value: 1548,
                        name: '工业'
                    }
	            ]
	        }]
    };
    $scope.option8 = {
    title: {
            text: '本年各部门备案情况',
            y: '30',
            x: 'center'
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        tooltip: {
            trigger: 'item',
            formatter: "{b} : {c} 件 ({d}%)"
        },

        toolbox: {
            show: false,
            feature: {
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: {
                    show: true
                },
                saveAsImage: {
                    show: true
                }
            }
        },
        calculable: false,
        series: [
            {
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            textStyle: {
                                fontFamily: 'MicrosoftYaHei',
                                fontSize: '12',
                                fontWeight: 'bold'
                            },
                            formatter: function (params) {
                                return (params.name) + ':' + params.value + ' (件)'
                            }
                        },
                        labelLine: {
                            show: true
                        }
                    },
                },
                data: [
                    {
                        value: 635,
                        name: '备案企业'
                    },
                    {
                        value: 1448,
                        name: '全部企业'
                    }
	            ]
	        }]
      };
    
    $scope.option9 = {
    title: {
            text: '累计各部门备案情况',
            y: '30',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{b} : {c} 件 ({d}%)"
        },
        color: colors,//['#DE3646', '#2AA9E3', '#ABD726', '#F68345', '#676567'],
        toolbox: {
            show: false,
            feature: {
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: {
                    show: true
                },
                saveAsImage: {
                    show: true
                }
            }
        },
        calculable: false,
        series: [
            {
                name: '地区占比',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            textStyle: {
                                fontFamily: 'MicrosoftYaHei',
                                fontSize: '12',
                                fontWeight: 'bold'
                            },
                            formatter: function (params) {
                                return (params.name) + ':' + params.value + ' (件)'
                            }
                        },
                        labelLine: {
                            show: true
                        }
                    },
                },
                data: [
                    {
                        value: 335,
                        name: '制造业'
                    },
                    {
                        value: 310,
                        name: '纺织业'
                    },
                    {
                        value: 234,
                        name: '服务业'
                    },
                    {
                        value: 135,
                        name: '农业'
                    },
                    {
                        value: 1548,
                        name: '工业'
                    }
	            ]
	        }]
       };
                    
    
	$scope.data = {};
	mtAjax("/dm/api/shorcut/home/allTitle").then(function (data) {
        $scope.data.summary = data;
    });
	
	mtAjax("/dm/api/shorcut/home/allChart").then(function (data) {
		
		// 协议备案情况
		var d11 = mtParseChartData(data[0], 'value1', 'date');
		var d12 = mtParseChartData(data[0], 'value2', 'date', undefined, d11.cols);
		$scope.data.chart1 = {
			'cols' : d11.cols,
			'rows' : ['备案协议件数', '涉及企业家数'],
			'data' : [d11.data[0], d12.data[0]],
		};
		
		// 政策兑现情况
		var d21 = mtParseChartData(data[1], 'value1', 'date', undefined, d11.cols);
		var d22 = mtParseChartData(data[1], 'value2', 'date', undefined, d11.cols);
		$scope.data.chart2 = {
			'cols' : d11.cols,
			'rows' : ['政策申报数量', '兑现通过数量'],
			'data' : [d21.data[0], d22.data[0]],
		};
		
		// 本年协议备案情况
		var d3cols = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ];
		var d31 = mtParseChartData(data[2], 'value1', 'date', undefined, d3cols);
		var d32 = mtParseChartData(data[2], 'value2', 'date', undefined, d3cols);
		$scope.data.chart3 = {
			'cols' : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
			'rows' : [ '备案协议件数', '涉及企业家数' ],
			'data' : [ d31.data[0]   , d32.data[0]    ],
		};
		
		// 本年政策兑现情况
		var d4cols = [ 1,2,3,4,5,6,7,8,9,10,11,12 ];
		var d41 = mtParseChartData(data[3], 'value1', 'date', undefined, d4cols);
		var d42 = mtParseChartData(data[3], 'value2', 'date', undefined, d4cols);
		$scope.data.chart4 = {
			'cols' : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
			'rows' : [ '政策申报数量', '兑现通过数量' ],
			'data' : [ d41.data[0]   , d42.data[0]    ],
		};
		
		// 协议备案：本年各部门备案情况
		$scope.data.chart5 = mtParseChartData(data[4], 'value1', 'type');
		
		// 协议备案：累计各部门备案情况
		$scope.data.chart6 = mtParseChartData(data[5], 'value1', 'type');
		
		// 协议备案：本年各行业备案情况
		$scope.data.chart7 = mtParseChartData(data[6], 'value1', 'type');
		
		// 协议备案：累计各行业备案情况
		$scope.data.chart8 = mtParseChartData(data[7], 'value1', 'type');
		
		// 兑现工作情况
		var d9 = mtParseChartData(data[8], 'value1', 'type');
		var v9 = [0];
		for (var i = 1; i < d9.data[0].length; ++i)
			v9[i] = v9[i-1] + d9.data[0][i-1];
		$scope.data.chart9 = {
			'cols' : d9.cols,
			'rows' : [ '辅助', '兑现工作情况' ],
			'data' : [ v9, d9.data[0] ],
		};
	});
	
	$scope.event = {};
	$scope.$watchCollection("event.chart1", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		if (newEvent.seriesIndex == 0) {
			// 年备案记录明细
			mtAjax("/dm/api/shorcut/home/detail/year/beian/proto", { year: newEvent.name }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal1.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		} else if (newEvent.seriesIndex == 1) {
			// 年备案企业明细
			mtAjax("/dm/api/shorcut/home/detail/year/beian/org", { year: newEvent.name }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal2.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		}
	});
	$scope.$watchCollection("event.chart2", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		if (newEvent.seriesIndex == 0) {
			// 年申报记录明细
			mtAjax("/dm/api/shorcut/home/detail/year/apply", { year: newEvent.name }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal3.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		} else if (newEvent.seriesIndex == 1) {
			// 年兑现记录明细
			mtAjax("/dm/api/shorcut/home/detail/year/apply/passed", { year: newEvent.name }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal4.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		}
	});
	$scope.$watchCollection("event.chart3", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		var months = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', ];
		if (newEvent.seriesIndex == 0) {
			// 月备案记录明细
			mtAjax("/dm/api/shorcut/home/detail/month/beian/proto", { month: months[newEvent.dataIndex] }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal5.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						month: function() { return newEvent.name },
						data : function() { return data[0]       },
					}
				});
			});
		} else if (newEvent.seriesIndex == 1) {
			// 月备案企业明细
			mtAjax("/dm/api/shorcut/home/detail/month/beian/org", { month: months[newEvent.dataIndex] }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal6.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						month: function() { return newEvent.name },
						data : function() { return data[0]       },
					}
				});
			});
		}
	});
	$scope.$watchCollection("event.chart4", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		var months = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', ];
		if (newEvent.seriesIndex == 0) {
			// 月申报记录明细
			mtAjax("/dm/api/shorcut/home/detail/month/apply", { month: months[newEvent.dataIndex] }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal7.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		} else if (newEvent.seriesIndex == 1) {
			// 月兑现记录明细
			mtAjax("/dm/api/shorcut/home2/detail/month/apply/passed", { month: months[newEvent.dataIndex] }).then(function(data) {
				var modalInstance = $uibModal.open({
					templateUrl : 'modal8.html',
					controller  : 'modalController',
					controllerAs: '$ctrl',
					size        : 'lg',
					resolve: {
						year: function() { return newEvent.name },
						data: function() { return data[0]       },
					}
				});
			});
		}
	});
	$scope.$watchCollection("event.chart5", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		// 本年备案记录明细
		mtAjax("/dm/api/shorcut/home/detail/dept/this/beian/proto", { dept: newEvent.name }).then(function(data) {
			var modalInstance = $uibModal.open({
				templateUrl : 'modal9.html',
				controller  : 'modalController',
				controllerAs: '$ctrl',
				size        : 'lg',
				resolve: {
					dept: function() { return newEvent.name },
					data: function() { return data[0]       },
				}
			});
		});
	});
	$scope.$watchCollection("event.chart6", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		// 本年备案记录明细
		mtAjax("/dm/api/shorcut/home/detail/dept/all/beian/proto", { dept: newEvent.name }).then(function(data) {
			var modalInstance = $uibModal.open({
				templateUrl : 'modal10.html',
				controller  : 'modalController',
				controllerAs: '$ctrl',
				size        : 'lg',
				resolve: {
					dept: function() { return newEvent.name },
					data: function() { return data[0]       },
				}
			});
		});
	});
	$scope.$watchCollection("event.chart7", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		// 本年备案记录明细
		mtAjax("/dm/api/shorcut/home/detail/industry/this/beian/proto", { industry: newEvent.name }).then(function(data) {
			var modalInstance = $uibModal.open({
				templateUrl : 'modal11.html',
				controller  : 'modalController',
				controllerAs: '$ctrl',
				size        : 'lg',
				resolve: {
					industry: function() { return newEvent.name },
					data: function() { return data[0]       },
				}
			});
		});
	});
	$scope.$watchCollection("event.chart8", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		// 本年备案记录明细
		mtAjax("/dm/api/shorcut/home/detail/industry/all/beian/proto", { industry: newEvent.name }).then(function(data) {
			var modalInstance = $uibModal.open({
				templateUrl : 'modal12.html',
				controller  : 'modalController',
				controllerAs: '$ctrl',
				size        : 'lg',
				resolve: {
					industry: function() { return newEvent.name },
					data: function() { return data[0]       },
				}
			});
		});
	});
	$scope.$watchCollection("event.chart9", function(newEvent, oldEvent) {
		if (!newEvent)
			return;
		
		// 兑现工作记录明细
		mtAjax("/dm/api/shorcut/home/detail/state/apply", { type: newEvent.name }).then(function(data) {
			var modalInstance = $uibModal.open({
				templateUrl : 'modal13.html',
				controller  : 'modalController',
				controllerAs: '$ctrl',
				size        : 'lg',
				resolve: {
					type: function() { return newEvent.name },
					data: function() { return data[0]       },
				}
			});
		});
	});
});

myApp.controller("modalController", function($scope, $uibModalInstance) {
	$ctrl = this;
	$ctrl.ok = function() {
		$uibModalInstance.close();
	};
});